package com.iwhalecloud.bss.kite.cucc.web;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;

import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

/**
 * @ClassName : ResponseExceptionHandler
 * @Description : 异常处理器
 * @Author : xie.zhengjun
 * @Date : 2020/9/30 5:10
 */
@RestControllerAdvice
public class ResponseExceptionHandler {

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(ResponseExceptionHandler.class);

    public ResponseExceptionHandler() {
        super();
    }

    /**
     * 业务异常HTTP状态设为200
     * 添加配置print.stackTrace=false即可关闭堆栈打印
     *
     * @param ex BssException
     * @return
     */
 /*   @ExceptionHandler(BssException.class)
    public ResponseEntity<DataResult> handleBssException(BssException ex) {
        LOGGER.error(ExceptionUtils.getStackTrace(ex));
        DataResult dataResult = new DataResult();
        dataResult.setResultCode(ex.getFailCode());
        dataResult.setResultMsg(ex.getFailMsg());
        this.setPrintTrace(dataResult, ex);
        return new ResponseEntity<>(dataResult, HttpStatus.OK);
    }
*/
    /**
     * 非业务异常500
     * 添加配置print.stackTrace=false即可关闭堆栈打印
     *
     * @param ex Exception
     * @return
     */
   /* @ExceptionHandler(Exception.class)
    public ResponseEntity<DataResult> handleException(Exception ex) {
        LOGGER.error(ExceptionUtils.getStackTrace(ex));
        DataResult dataResult = new DataResult();
        dataResult.setResultCode(HttpStatus.INTERNAL_SERVER_ERROR.toString());
        dataResult.setResultMsg(ex.getMessage());
        this.setPrintTrace(dataResult, ex);
        return new ResponseEntity<>(dataResult, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    private void setPrintTrace(DataResult dataResult, Throwable ex) {
        String isPrint = SpringUtil.getApplicationContext().getEnvironment().getProperty("print.stackTrace");
        if (KiteStringUtils.isEmpty(isPrint) || Boolean.parseBoolean(isPrint)) {
            dataResult.setResultDetail(ExceptionUtils.getStackTrace(ex));
        }
    }*/
}
